package com.itheima.itheima_health.controller.admin.BookingController;

import com.itheima.itheima_health.constant.MessageConstant;
import com.itheima.itheima_health.entity.PageResult;
import com.itheima.itheima_health.entity.QueryPageBean;
import com.itheima.itheima_health.entity.Result;
import com.itheima.itheima_health.pojo.CheckGroup;
import com.itheima.itheima_health.pojo.CheckItem;
import com.itheima.itheima_health.service.CheckitemGroupService;
import com.itheima.itheima_health.service.CheckitemTermService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查组相关接口
 */
@RestController
@RequestMapping("/checkitemgroup")
@Slf4j
@Api(tags = "检查组相关接口")
public class CheckitemGroupController {

    @Autowired
    private CheckitemGroupService checkitemGroupService;
    @Autowired
    private CheckitemTermService bookingService;

    /**
     * 检查组分页查询
     * @param queryPageBean
     * @return
     */
    @ApiOperation("检查组分页查询")
    @PostMapping("/pageQuery")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")
    public PageResult CheckGroupPageQuery(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}",queryPageBean);
        PageResult pageResult = checkitemGroupService.CheckitemGropPageQuery(queryPageBean);
        return pageResult;
    }

    /**
     * 检查组新增页面中回显检查项数据
     * @return
     */
    @ApiOperation("检查组新增页面中回显检查项数据")
    @GetMapping("/allcheckitem")
    public Result getallCheckitem(){
        log.info("检查组新增页面中回显检查项数据");
        List<CheckItem> checkItemList = bookingService.getallCheckitem();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItemList);
    }

    /**
     * 添加检查组
     * @param checkGroup
     * @param checkitemIds
     * @return
     */
    @ApiOperation("添加检查组")
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_ADD')")
    public Result CheckitemGroupAdd(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("新增检查组:{} 该检查组的检查项ids:{}",checkGroup,checkitemIds);
        checkitemGroupService.CheckitemGroupAdd(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }
    /**
     * 根据id回显检查组数据
     * @param id
     * @return
     */
    @ApiOperation("根据id回显检查组数据")
    @GetMapping("/findById/{id}")
    public Result CheckitemGroupfindById(@PathVariable Long id){
        log.info("根据id回显检查组数据");
        CheckGroup checkGroup = checkitemGroupService.CheckitemGroupfindById(id);
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }

    /**
     * 根据检查组id查询该检查组的检查项
     * @param id
     * @return
     */
    @ApiOperation("根据检查组id查询该检查组的检查项")
    @GetMapping("/findCheckitemgroupIdBycheckitemId/{id}")
    @PreAuthorize("hasAnyAuthority('CHECKITEM_QUERY')")
    public Result findCheckitemgroupIdBycheckitemId(@PathVariable Long id){
        log.info("根据检查组id查询该检查组的检查项:{}",id);
        List<Integer> checkitemIds = checkitemGroupService.findCheckitemgroupIdBycheckitemId(id);
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkitemIds);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @return
     */
    @ApiOperation("编辑检查组")
    @PutMapping("/update")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_EDIT')")
    public Result update(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("编辑检查组   检查组:{},检查项:{}",checkGroup,checkitemIds);
        checkitemGroupService.update(checkGroup,checkitemIds);
        return new Result(true,MessageConstant.EDIT_CHECKGROUP_SUCCESS);
    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @ApiOperation("删除检查组")
    @DeleteMapping("/delete/{id}")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_DELETE')")
    public Result delete(@PathVariable Integer id){
        log.info("删除检查组id:{}",id);
        checkitemGroupService.delete(id);
        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    /**
     * 查询全部检查组
     * @return
     */
    @ApiOperation("查询全部检查组")
    @GetMapping("/findAllCheckGroup")
    public Result findAllCheckGroup(){
        log.info("查询全部检查组");
        List<CheckGroup> checkGroupList = checkitemGroupService.findAllCheckGroup();
        return new Result(true,MessageConstant.QUERY_CHECKGROUP_SUCCESS,checkGroupList);
    }
}
